//
//  ForgetPasswordViewController.m
//  Manage
//
//  Created by 杨康 on 2017/11/1.
//  Copyright © 2017年 杨康. All rights reserved.
//

#import "ForgetPasswordViewController.h"
#import "QCCountdownButton.h"

@interface ForgetPasswordViewController ()
{
    YKTextField *_phoneTF;
    QCCountdownButton *_codeBtn;
}
@end

@implementation ForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"忘记密码";
    
    //手机号
    YKTextField *phoneTF=[SingLeton myTextFieldsecure:NO andkeyboardType:UIKeyboardTypeNumberPad andplaceholder:@"请输入手机号" andLeftImage:nil RequestSuccess:^(YKTextField *textField, NSRange range, NSString *string) {
        
            
            
            NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
            if (text.length==11) {
                //获取验证码按钮变蓝，可点击
                _codeBtn.enabled=YES;
                
            }else
            {
                //获取验证码按钮变灰，不可点击
                _codeBtn.enabled=NO;
            }
        
        
    }];
    _phoneTF=phoneTF;
    [self.view addSubview:phoneTF];
    
    [phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(30+TOP_HEIGHT);
        make.left.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-40, 44));
    }];
    
    //验证码
    YKTextField *verifyCodeTF=[SingLeton myTextFieldsecure:NO andkeyboardType:UIKeyboardTypeDefault andplaceholder:@"请输入收到的验证码" andLeftImage:nil RequestSuccess:^(YKTextField *textField, NSRange range, NSString *string) {
        
    }];
    [self.view addSubview:verifyCodeTF];
    
    [verifyCodeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(phoneTF.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-40-90, 44));
    }];
    
    //获取验证码按钮
    QCCountdownButton *btn = [QCCountdownButton countdownButton];
    btn.title = @"发送验证码";
    _codeBtn=btn;
    //开始不可点击
    _codeBtn.enabled=NO;
    
    // 倒计时的时长
    btn.totalSecond = 120;
    // 字体
    btn.titleLabelFont = [UIFont systemFontOfSize:14];
    
    [self.view addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(85,44));
        make.centerY.equalTo(verifyCodeTF);
        make.left.equalTo(verifyCodeTF.mas_right).mas_offset(5);
    }];
    //进度
    [btn onStartBlock:^{
        [self.view endEditing:YES];
        [self judgeCondition:btn];
    }processBlock:^(NSUInteger second){
       //输入框不能操作
        _phoneTF.enabled=NO;
        btn.title = [NSString stringWithFormat:@"剩余%li秒", (unsigned long)second] ;
    } onFinishedBlock:^{
        //输入框能操作
        _phoneTF.enabled=YES;
        // 倒计时完毕
        btn.title = @"重新获取";
    }];
    
    
    //登录密码
    YKTextField *passwordTF=[SingLeton myTextFieldsecure:YES andkeyboardType:UIKeyboardTypeDefault andplaceholder:@"请输入登录密码" andLeftImage:nil RequestSuccess:^(YKTextField *textField, NSRange range, NSString *string) {
    }];
    
    [self.view addSubview:passwordTF];
    [passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(verifyCodeTF.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-40, 44));
    }];
    
    
    //登录密码(两次输入)
    YKTextField *passwordTF2=[SingLeton myTextFieldsecure:YES andkeyboardType:UIKeyboardTypeDefault andplaceholder:@"请再次输入登录密码" andLeftImage:nil RequestSuccess:^(YKTextField *textField, NSRange range, NSString *string) {
    }];
    
    [self.view addSubview:passwordTF2];
    [passwordTF2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(passwordTF.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-40, 44));
    }];
    
    
    //确定按钮
    UIButton  *confirmButton=[SingLeton endButton:[UIColor colorQ] andTitle:@"确定" RequestSuccess:^(UIButton *btn) {
        
    }];
    [self.view addSubview:confirmButton];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(passwordTF2.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(passwordTF2);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-40, 44));
    }];
    
    
    
    
    
    
    
    
    
    
    
}

-(void)judgeCondition:(QCCountdownButton *)btn
{
    NSString *tel = _phoneTF.text;
    
    if([NSString isNULLString:tel]){
        
        [self.view makeToast:@"请输入手机号码"];
        [btn stopTime];
        
    }else
    {
        [self requestYZM:tel];
    }
    
    
    
    
}
#pragma mark 获取验证码请求
-(void)requestYZM:(NSString *)str
{
    NSDictionary * postDic;
    
    NSString *url=[NSString stringWithFormat:@"%@/app/user/generateSMSCode",APPBaseUrl];
    
    [NetworkPath requestWithMethod:0 andURLString:url andParameters:postDic  RequestSuccess:^(NSURLResponse *response, id responseObject)
     {
         LRLog(@"responseObject\n%@",responseObject);
         NSString *code = responseObject[@"code"];
         //如果code不存在，会不会崩溃
         if([code isEqualToString:@"10008"]){
             
             [self.view makeToast:@"验证码已发送"];
         }else{
             [_codeBtn stopTime];
             NSString *str=responseObject[@"msg"];
             
             [self.view makeToast:str];
         }
     } RequestFailed:^(NSError *error) {
         [_codeBtn stopTime];
         
         [self.view makeToast:NetError];
     }];
}


@end
